package com.tjec.project.businessStandardLibrary.controller;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.tjec.common.constant.Constants;
import com.tjec.common.utils.StringUtils;
import com.tjec.common.utils.sql.SqlUtil;
import com.tjec.framework.web.page.PageDomain;
import com.tjec.framework.web.page.TableSupport;
import com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeBusinessGuide;
import com.tjec.project.system.domain.BmsAttachment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeCase;
import com.tjec.project.businessStandardLibrary.service.IBmsKnowledgeCaseService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * 典型案例Controller
 * 
 * @author pangyongfeng
 * @date 2020-04-21
 */
@Api("典型案例")
@RestController
@RequestMapping("/library/case")
public class BmsKnowledgeCaseController extends BaseController {
	@Autowired
	private IBmsKnowledgeCaseService bmsKnowledgeCaseService;
	@Value("${sftp.http}")
	private String http;
	@Value("${sftp.ip}")
	private String ip;

	/**
	 * 查询典型案例列表
	 */
	@ApiOperation("查询典型案例列表")
	@PreAuthorize("@ss.hasPermi('library:case:list')")
	@GetMapping("/list")
	public TableDataInfo list(BmsKnowledgeCase bmsKnowledgeCase) {
		startPage();
		List<BmsKnowledgeCase> list = bmsKnowledgeCaseService.selectBmsKnowledgeCaseList(bmsKnowledgeCase);
		if (list != null && list.size() > 0) {
			for (BmsKnowledgeCase knowledgeCase : list) {
				addResourcePrefix(knowledgeCase);
			}
		}

		return getDataTable(list);
	}

	/**
	 * 导出典型案例列表
	 */
	@ApiOperation("导出典型案例列表")
	@PreAuthorize("@ss.hasPermi('library:case:export')")
	@Log(title = "典型案例", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BmsKnowledgeCase bmsKnowledgeCase) {
		List<BmsKnowledgeCase> list = bmsKnowledgeCaseService.selectBmsKnowledgeCaseList(bmsKnowledgeCase);
		ExcelUtil<BmsKnowledgeCase> util = new ExcelUtil<BmsKnowledgeCase>(BmsKnowledgeCase.class);
		return util.exportExcel(list, "case");
	}

	/**
	 * 获取典型案例详细信息
	 */
	@ApiOperation("获取典型案例详细信息")
	@PreAuthorize("@ss.hasPermi('library:case:query')")
	@GetMapping(value = "/{guid}")
	public AjaxResult getInfo(@PathVariable("guid") String guid) {
		BmsKnowledgeCase res = bmsKnowledgeCaseService.selectBmsKnowledgeCaseByGuid(guid);
		if (res != null) {
			addResourcePrefix(res);
		}

		return AjaxResult.success(res);
	}

	/**
	 * 新增典型案例
	 */
	@ApiOperation("新增典型案例")
	@PreAuthorize("@ss.hasPermi('library:case:add')")
	@Log(title = "典型案例", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BmsKnowledgeCase bmsKnowledgeCase) {
		return toAjax(bmsKnowledgeCaseService.insertBmsKnowledgeCase(bmsKnowledgeCase));
	}

	/**
	 * 修改典型案例
	 */
	@ApiOperation("修改典型案例")
	@PreAuthorize("@ss.hasPermi('library:case:edit')")
	@Log(title = "典型案例", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BmsKnowledgeCase bmsKnowledgeCase) {
		return toAjax(bmsKnowledgeCaseService.updateBmsKnowledgeCase(bmsKnowledgeCase));
	}

	/**
	 * 删除典型案例
	 */
	@ApiOperation("删除典型案例")
	@PreAuthorize("@ss.hasPermi('library:case:remove')")
	@Log(title = "典型案例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
	public AjaxResult remove(@PathVariable String[] guids) {
		return toAjax(bmsKnowledgeCaseService.deleteBmsKnowledgeCaseByGuids(guids));
	}

	/**
	 * 查询典型案例列表 带上树
	 */
	@ApiOperation("查询典型案例列表")
	@PreAuthorize("@ss.hasPermi('library:case:list')")
	@GetMapping("/selList")
	public TableDataInfo SelList(BmsKnowledgeCase bmsKnowledgeCase) {
		startPage();
		List<BmsKnowledgeCase> list = bmsKnowledgeCaseService.selectBmsKnowledgeCaseTreeList(bmsKnowledgeCase);
		for (BmsKnowledgeCase knowledgeCase : list) {
			addResourcePrefix(knowledgeCase);
		}
		return getDataTable(list);
	}

	/**
	 * villwang 获取典型案例详细信息 详情
	 */
	@ApiOperation("获取典型案例详细信息")
	@PreAuthorize("@ss.hasPermi('library:case:query')")
	@GetMapping(value = "/details")
	public AjaxResult details(String guid) {
		BmsKnowledgeCase bmsKnowledgeCase = bmsKnowledgeCaseService.selectBmsKnowledgeCaseByGuiddetails(guid);
		addResourcePrefix(bmsKnowledgeCase);
		return AjaxResult.success("操作成功", bmsKnowledgeCase);
	}

	/**
	 * 附件路径添加前缀
	 * 
	 * @param bmsKnowledgeCase
	 * @return
	 */
	private void addResourcePrefix(BmsKnowledgeCase bmsKnowledgeCase) {
		if (!CollectionUtils.isEmpty(bmsKnowledgeCase.getAttachments())) {
			for (BmsAttachment bmsAttachment : bmsKnowledgeCase.getAttachments()) {
				if (bmsAttachment != null && StringUtils.isNotBlank(bmsAttachment.getStoragePath())) {
					bmsAttachment.setStoragePath(http + ip +Constants.RESOURCE_PREFIX + bmsAttachment.getStoragePath());
				}
			}
		}
	}

}
